package com.liuxinlong.modules.patent.controller;

import cn.hutool.json.JSONObject;
import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.enums.ProjectDealEnum;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.PatentRisk;
import com.liuxinlong.modules.patent.controller.request.QueryPatentProposalReq;
import com.liuxinlong.modules.patent.controller.request.QueryPatentRiskReq;
import com.liuxinlong.modules.patent.controller.request.UpdatePatentProjectAssistReq;
import com.liuxinlong.modules.patent.controller.request.UpdatePatentProposalProcessReq;
import com.liuxinlong.modules.patent.controller.request.UpdatePatentRiskProcessReq;
import com.liuxinlong.modules.patent.controller.request.UpdatePatentRiskReq;
import com.liuxinlong.modules.patent.controller.request.UpdatePatentRiskSearchReq;
import com.liuxinlong.modules.patent.controller.request.UpdatePatentRiskStatusReq;
import com.liuxinlong.modules.patent.service.PatentProposalService;
import com.liuxinlong.modules.patent.service.PatentRiskProcessService;
import com.liuxinlong.modules.patent.service.PatentRiskService;
import com.liuxinlong.utils.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 专利风险管控控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2024-03-15
 */
@RestController
@RequestMapping("/patent-risk")
public class PatentRiskController {

    @Autowired
    private PatentRiskService patentRiskService;

    @Autowired
    private PatentRiskProcessService patentRiskProcessService;

    @Autowired
    private PatentProposalService patentProposalService;

    /**
     * 分页查询专利风险管控信息
     *
     * @param req 请求信息
     * @return 专利风险管控信息列表
     */
    @PostMapping("project-page")
    @LogAnnotation(value = "分页查询", business = LogBusinessConstants.PATENT_RISK, operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp pagePatentProjectInfo(@RequestBody QueryPatentRiskReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("projectName", req.getProjectName());
        queryMap.put("department", req.getDepartment());
        queryMap.put("projectType", req.getProjectType());
        queryMap.put("projectLevel", req.getProjectLevel());
        queryMap.put("monitor", req.getMonitor());
        queryMap.put("projectManager", req.getProjectManager());
        queryMap.put("dealStatus", ProjectDealEnum.VALID_DATA.getValue());
        queryMap.put("available", 1);
        String projectRisk = req.getProjectRisk();
        if (!StringUtils.isEmpty(projectRisk)) {
            List<String> originalList = Arrays.asList(projectRisk.split(","));
            List<Integer> projectRiskList = originalList.stream().map(Integer::parseInt).collect(Collectors.toList());
            queryMap.put("projectRiskList", projectRiskList);
        }
        List<Map<String, Object>> dataList = patentRiskService.pagePatentProjectInfo(queryMap);
        int total = patentRiskService.countPatentProject(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询专利项目信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(total);
        return resp;
    }

    /**
     * 分页查询专利风险管控信息
     *
     * @param req 请求信息
     * @return 专利风险管控信息列表
     */
    @PostMapping("page")
    @LogAnnotation(value = "分页查询", business = LogBusinessConstants.PATENT_RISK, operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp pagePatentRiskInfo(@RequestBody QueryPatentRiskReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("projectId", req.getProjectId());
        List<Map<String, Object>> dataList = patentRiskService.pagePatentRiskInfo(queryMap);
        int total = patentRiskService.countPatentRisk(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询专利风险管控信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(total);
        return resp;
    }

    /**
     * 新增专利风险管控信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value = "新增专利风险管控信息", business = LogBusinessConstants.PATENT_RISK, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addPatentRiskInfo(@RequestBody UpdatePatentRiskReq req) {
        PatentRisk patentRisk = new PatentRisk();
        patentRisk.setRiskName(req.getRiskName());
        patentRisk.setProjectId(req.getProjectId());
        patentRisk.setReferenceModel(req.getReferenceModel());
        patentRisk.setPicture(req.getPicture());
        patentRisk.setOperateUser(req.getOperateUser());
        patentRisk.setSearchConclusion(req.getSearchConclusion());
        patentRisk.setPlanSourceInfo(req.getPlanSourceInfo());
        patentRisk.setRemark(req.getRemark());
        patentRiskService.addPatentRiskInfo(patentRisk);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增专利风险管控信息成功");
        return resp;
    }

    /**
     * 修改专利风险管控信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "修改专利风险管控信息", business = LogBusinessConstants.PATENT_RISK, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updatePatentRiskInfo(@RequestBody UpdatePatentRiskReq req) {
        PatentRisk patentRisk = new PatentRisk();
        patentRisk.setId(req.getId());
        patentRisk.setRiskName(req.getRiskName());
        patentRisk.setProjectId(req.getProjectId());
        patentRisk.setReferenceModel(req.getReferenceModel());
        patentRisk.setPicture(req.getPicture());
        patentRisk.setOperateUser(req.getOperateUser());
        patentRisk.setSearchConclusion(req.getSearchConclusion());
        patentRisk.setPlanSourceInfo(req.getPlanSourceInfo());
        patentRisk.setRemark(req.getRemark());
        patentRiskService.updatePatentRiskInfo(patentRisk);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改专利风险管控信息成功");
        return resp;
    }

    /**
     * 删除专利风险管控信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除专利风险管控信息", business = LogBusinessConstants.PATENT_RISK, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deletePatentRiskInfo(@RequestBody BaseDeleteReq req) {
        patentRiskService.deletePatentRiskInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除专利风险管控信息成功");
        return resp;
    }

    /**
     * 修改专利风险管控状态信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("status")
    @LogAnnotation(value = "修改专利风险管控状态信息", business = LogBusinessConstants.PATENT_RISK, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updatePatentRiskStatus(@RequestBody UpdatePatentRiskStatusReq req) {
        patentRiskService.updatePatentRiskStatus(req.getId(), req.getType(), req.getTargetStatus());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改专利风险管控状态信息成功");
        return resp;
    }

    /**
     * 项目专利排查流程发起
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("investigation-start")
//    @PostMapping("project-start")
    @LogAnnotation(value = "项目专利排查流程发起", business = LogBusinessConstants.PATENT_RISK, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp startProjectInvestigationProcess(@RequestBody UpdatePatentRiskProcessReq req) {
//        JSONObject data = new JSONObject(req);
//        String dataString = data.toJSONString(2);
        Map<String, Object> dataMap = ObjectUtils.object2Map(req);
        dataMap.remove("changeList");
        JSONObject data = new JSONObject(dataMap);
        String dataString = data.toJSONString(2);
        patentRiskService.startPatentRiskProcess(req.getId(), 1, dataString, req.getChangeList());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("项目专利排查流程发起成功");
        return resp;
    }

    /**
     * 项目风险管控流程发起
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("project-start")
//    @PostMapping("patent-start")
    @LogAnnotation(value = "项目风险管控流程发起", business = LogBusinessConstants.PATENT_RISK, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp startPatentProjectProcess(@RequestBody UpdatePatentRiskProcessReq req) {
//        JSONObject data = new JSONObject(req);
//        String dataString = data.toJSONString(2);
        Map<String, Object> dataMap = ObjectUtils.object2Map(req);
        dataMap.remove("changeList");
        JSONObject data = new JSONObject(dataMap);
        String dataString = data.toJSONString(2);
        patentRiskService.startPatentRiskProcess(req.getId(), 2, dataString, null);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("项目风险管控流程发起成功");
        return resp;
    }

    /**
     * 专利风险管控流程发起
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("patent-start")
    @LogAnnotation(value = "专利风险管控流程发起", business = LogBusinessConstants.PATENT_RISK, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp startPatentRiskProcess(@RequestBody UpdatePatentProposalProcessReq req) {
//        JSONObject data = new JSONObject(req);
//        String dataString = data.toJSONString(2);
        Map<String, Object> dataMap = ObjectUtils.object2Map(req);
        dataMap.remove("changeList");
        JSONObject data = new JSONObject(dataMap);
        String dataString = data.toJSONString(2);
        patentProposalService.startWorkflow(req.getId(), dataString, null);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("专利风险管控流程发起成功");
        return resp;
    }

    /**
     * 设计差异点检索式导入
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("record-update")
    @LogAnnotation(value = "设计差异点检索式导入", business = LogBusinessConstants.PATENT_RISK, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateRiskSearchRecord(@RequestBody UpdatePatentRiskSearchReq req) {
        patentRiskService.updateRiskSearchRecord(req.getId(), req.getFileId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("设计差异点检索式导入成功");
        return resp;
    }

//    /**
//     * 专利风险管控数据详情查询
//     *
//     * @param req 请求信息
//     * @return 成功标识
//     */
//    @PostMapping("investigation-page")
//    @LogAnnotation(value = "专利风险管控数据详情查询", business = LogBusinessConstants.PATENT_RISK, operateType = LogOperateConstants.OPERATE_QUERY)
//    public BaseResp getProjectInvestigation(@RequestBody BaseDeleteReq req) {
//        Map<String, Object> dataMap = patentRiskService.getProjectInvestigation(req.getId());
//        BaseResp resp = new BaseResp();
//        resp.setDataMap(dataMap);
//        resp.setRetMessage("专利风险管控数据详情查询成功");
//        return resp;
//    }

    /**
     * 专利风险管控过程数据查询
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("process-page")
    @LogAnnotation(value = "专利风险管控过程数据查询", business = LogBusinessConstants.PATENT_RISK, operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp getPatentRiskProcess(@RequestBody QueryBaseReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("projectId", req.getId());
        queryParam.put("startNum", 0);
        queryParam.put("pageSize", 100);
        List<Map<String, Object>> dataList = patentRiskProcessService.pagePatentRiskProcessInfo(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setRetMessage("专利风险管控过程数据查询成功");
        return resp;
    }

    /**
     * 设计差异点检索式导入
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("assist-update")
    @LogAnnotation(value = "项目协助人修改", business = LogBusinessConstants.PATENT_RISK, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateProjectAssist(@RequestBody UpdatePatentProjectAssistReq req) {
        patentRiskService.updateProjectAssist(req.getId(), req.getProjectAssist());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("项目协助人修改成功");
        return resp;
    }

    /**
     * 查看项目完整报告
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("complete-report")
    @LogAnnotation(value = "查看项目完整报告", business = LogBusinessConstants.PATENT_RISK, operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp queryProjectCompleteReport(@RequestBody QueryBaseReq req) {
        Map<String,Object> dataMap = patentRiskService.queryProjectCompleteReport(req.getId());
        BaseResp resp = new BaseResp();
        resp.setDataMap(dataMap);
        resp.setRetMessage("查看项目完整报告成功");
        return resp;
    }

    /**
     * 导出项目完整报告
     *
     * @param response response
     * @return 导出数据
     */
    @PostMapping("complete-export")
    @LogAnnotation(value = "导出项目完整报告", business = LogBusinessConstants.PATENT_RISK, operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportProjectCompleteReport(@RequestBody QueryBaseReq req, HttpServletResponse response) {
        patentRiskService.exportProjectCompleteReport(req.getId(),response);
    }

    /**
     * 项目风险专利信息
     *
     * @param req 请求信息
     * @return 项目风险专利信息列表
     */
    @PostMapping("proposal-page")
    public BaseResp pagePatentProjectProposalInfo(@RequestBody QueryPatentProposalReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("department", req.getDepartment());
        queryMap.put("projectId", req.getProjectId());
        queryMap.put("riskId", req.getRiskId());
        queryMap.put("patentName", req.getPatentName());
        queryMap.put("patentSn", req.getPatentSn());
        queryMap.put("riskLevel", req.getRiskLevel());
        queryMap.put("projectManager", req.getProjectManager());
        queryMap.put("monitor", req.getMonitor());
        List<Map<String, Object>> dataList = patentProposalService.pagePatentProposalInfo(queryMap);
        int total = patentProposalService.countPatentProposal(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询风险专利信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(total);
        return resp;
    }
}
